define(function(){
	function trolley(){
		console.log($.cookie("article"));
		var cookieStr = $.cookie("MCLuserCookie");
		if(cookieStr){
			$("#top .ul1").css("display","none");
			$("#top .ul2").css("display","block");
		}else{
			$("#top .ul1").css("display","block");
			$("#top .ul2").css("display","none");
		}
		//退出按钮
		$(".quit").click(function(){
			$("#top .ul1").css("display","block");
			$("#top .ul2").css("display","none");
			$(this).attr("href","index.html");
		})
			var cookieStr = $.cookie("article") ? $.cookie("article") : "";
			var cookieObj0 = convertcookieStrToCookieObj(cookieStr);
			var count = 0;
			var money = 0;
			for(var i in cookieObj0){
				count += cookieObj0[i].num;
				money += cookieObj0[i].num * cookieObj0[i].price;
			}
			$(".money").html(money);
			$(".cases").html(count);
			$("input").attr("checked",false);
		if(!cookieStr){
			$(".hint_txt").css("display","none");
		}else{
			var cookieObj = convertcookieStrToCookieObj(cookieStr);
			console.log(cookieObj);
			for(var id in cookieObj){
				var goodId = cookieObj[id];
				var str = `<ul class="goods" data_id = "${id}">
							<li>
								<input type="checkbox" class="xuanzhong choice"/>
							</li>
							<li>
								<a href="#" class="thumbnail">	
									<span><img src = "${goodId.src}"</span>
								</a>
								<a href="#" class="name_one">${goodId.name}</a>
								<i class="number"><span>编号：</span>${goodId.numbers}</i>
							</li>
							<li class="cost">${goodId.price}</li>
							<li class="count">
								<p>
									<a href="javascript:;" class="minus">-</a>
									<b class="sum">${goodId.num}</b>
									<a href="javascript:;" class="plus">+</a>
								</p>
							</li>
							<li class="subtotal">${parseFloat(goodId.price) * goodId.num}</li>
							<li><a href="javascript:;" class="del">删除</a></li>
						</ul>`
				$(str).appendTo($(".detail"));
			}
		}
		$(".goods a.del").click(function(){
			var answer = confirm("确定删除该商品？");
			if(answer){
				//查找删除元素的id
				var id = $(this).parents(".goods").remove().attr("data_id");
				$(this).parents(".goods").remove();
				var cookieStr1 = $.cookie("article") ? $.cookie("article") : "";
				var cookieObjs = convertcookieStrToCookieObj(cookieStr1);
				//delete
				delete cookieObjs[id];
				$.cookie("article",convertObjToCarStr(cookieObjs),{expires : 7,path:"/"});
				//同步数据
				var count = 0;
				var money = 0;
				for(var i in cookieObjs){
					count += cookieObjs[i].num;
					money += cookieObjs[i].num * cookieObjs[i].price;
				}
				$(".money").html(money);
				$(".cases").html(count);
			}
		})
		//加号添加点击事件
		$(".goods .count .plus").click(function(){
			var id = $(this).parents(".goods").attr("data_id");
			var cookieStr2 = $.cookie("article") ? $.cookie("article") : "";
			var cookieObj2 = convertcookieStrToCookieObj(cookieStr2);
			cookieObj2[id].num += 1;
			$(this).siblings(".sum").html(cookieObj2[id].num);
			$(this).parents(".count").siblings(".subtotal").html(cookieObj2[id].num * parseFloat(cookieObj2[id].price));
			$.cookie("article",convertObjToCarStr(cookieObj2),{expires : 7,path : "/"});
			//数量总计
			$(".cases").html(cookieObj2[id].num);
			$(".money").html(cookieObj2[id].num * parseFloat(cookieObj2[id].price))
		})
		//减号添加点击事件
		$(".goods .count .minus").click(function(){
			var id = $(this).parents(".goods").attr("data_id");
			var cookieStr2 = $.cookie("article") ? $.cookie("article") : "";
			var cookieObj2 = convertcookieStrToCookieObj(cookieStr2);
			//数量总计
			if(cookieObj2[id].num > 1){
				cookieObj2[id].num -= 1;
				$(this).siblings(".sum").html(cookieObj2[id].num);
				$(this).parents(".count").siblings(".subtotal").html(cookieObj2[id].num * parseFloat(cookieObj2[id].price));
				$.cookie("article",convertObjToCarStr(cookieObj2),{expires : 7,path : "/"});
				$(".cases").html(cookieObj2[id].num);
				$(".money").html(cookieObj2[id].num * parseFloat(cookieObj2[id].price))
			}
		})
		//全选事件
		$(".checkFull").click(function(){
			if($(this).prop("checked")){
				$(".xuanzhong").each(function(){
					$(this).prop("checked","checked");
				})
			}else{
				$(".xuanzhong").each(function(){
					$(this).prop("checked",false);					
				})
			}
			var cookieStr = $.cookie("article") ? $.cookie("article") : "";
			var cookieObj = convertcookieStrToCookieObj(cookieStr);
			var count = 0;
			for(var i in cookieObj){
				count += cookieObj[id].num
			}
			$(".cases").html(count);
		})
		//清空选中的商品
		$(".clears").click(function(){
			$(".choice").each(function(index,value){
				if($(this).prop("checked")){
					var cookieStr = $.cookie("article") ? $.cookie("article") : "";
					var cookieObj = convertcookieStrToCookieObj(cookieStr);
					var id = $(this).parents(".goods").attr("data_id");
					delete cookieObj[id];
					$.cookie("article",convertObjToCarStr(cookieObj),{expires : 7,path : "/"});
					$(".goods").load(true);
				}else{
					alert("现在没有选中的商品");
				}
			})
		})
		function convertcookieStrToCookieObj(cookieStr){
			if(!cookieStr){
				return {};
			}
		var goods = cookieStr.split(":");
		var obj = {};
		for(var i = 0;i < goods.length;i ++){
			var data = goods[i].split(",");
			obj[data[0]] = {
				src : data[1],
				name : data[2],
				numbers : data[3],
				num : parseInt(data[4]),
				price : parseFloat(data[5])
			}
		}
		return obj;
	}
	function convertObjToCarStr(obj){
		var cookieStrs = "";
		for(var id in obj){
			if(cookieStrs){
				cookieStrs += ":";
			}
			cookieStrs += id + "," + obj[id].src + "," + obj[id].name + "," + obj[id].numbers + "," + obj[id].num + "," + obj[id].price;
		}
		return cookieStrs;
	}
	}
	return {
		trolley : trolley
	}
})